//
//  ViewController.swift
//  like_animation
//
//  Created by 劢克科技 on 2019/3/14.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    // 点赞框：一个自定义的View
    // 内部定义好了大小以及加入了表情。使用时设置位置即可。
    fileprivate lazy var likeContainer: UIView = {
        let container = UIView()
        let iconHeight: CGFloat = 38
        let margin: CGFloat = 6
        let images = [#imageLiteral(resourceName: "blue_like"), #imageLiteral(resourceName: "red_heart"), #imageLiteral(resourceName: "surprised"), #imageLiteral(resourceName: "cry_laugh"), #imageLiteral(resourceName: "cry"), #imageLiteral(resourceName: "angry")]

        let arrangedSubviews = images.map({ (image) -> UIView in
            let imageV = UIImageView(image: image)
            imageV.layer.cornerRadius = iconHeight / 2
            imageV.isUserInteractionEnabled = true
            return imageV
        })
        
        let stackView = UIStackView(arrangedSubviews: arrangedSubviews)
        stackView.distribution = .fillEqually
        stackView.spacing = margin
        stackView.layoutMargins = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
        stackView.isLayoutMarginsRelativeArrangement = true
        
        container.backgroundColor = UIColor.white
        container.layer.cornerRadius = (iconHeight + margin * 2) / 2
        container.layer.shadowColor = UIColor(white: 0.4, alpha: 0.4).cgColor
        container.layer.shadowRadius = 8
        container.layer.shadowOpacity = 0.4
        container.layer.shadowOffset = CGSize(width: 0, height: 4)
        container.frame = CGRect(x: 0, y: 0, width: (CGFloat(arrangedSubviews.count) * iconHeight) + CGFloat(arrangedSubviews.count + 1) * margin, height: iconHeight + margin * 2)
        
        stackView.frame = container.bounds
        container.addSubview(stackView)
        return container
    }()
    
    //MARK: - Life Circle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let backgroundView = UIImageView(image: #imageLiteral(resourceName: "fb_core_data_bg"))
        backgroundView.frame = view.bounds
        view.addSubview(backgroundView)
        
        setLongPress()
    }
    
    // 隐藏StatusBar
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    /// 给View添加长按的手势
    fileprivate func setLongPress() {
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(press:)))
        view.addGestureRecognizer(longPress)
    }
    
    
    /// 长按手势调用的方法
    ///
    /// - Parameter press: UILongPressGestureRecognizer
    @objc func longPress(press: UILongPressGestureRecognizer) {
        
        if press.state == .began {
            handleGestureBegan(press: press)
        } else if press.state == .ended {
            handleGestureEnd(press: press)
        } else if press.state == .changed {
            handleGestureChange(press: press)
        }
    }
    
    /// 长按后显示点赞框
    ///
    /// - Parameter press: UILongPressGestureRecognizer
    fileprivate func handleGestureBegan(press: UILongPressGestureRecognizer) {
        let location = press.location(in: view)
        view.addSubview(likeContainer)
        
        let centerX = (UIScreen.main.bounds.width - likeContainer.frame.width) / 2
        likeContainer.alpha = 0
        self.likeContainer.transform = CGAffineTransform(translationX: centerX, y: location.y)
        
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
            self.likeContainer.transform = CGAffineTransform(translationX: centerX, y: location.y - self.likeContainer.frame.height - 20)
            self.likeContainer.alpha = 1
        })
    }
    
    
    /// 处理长按移动的事件
    ///
    /// - Parameter press: UILongPressGestureRecognizer
    fileprivate func handleGestureChange(press: UILongPressGestureRecognizer) {
        let location = press.location(in: likeContainer)

        let fixYLocation = CGPoint(x: location.x, y: likeContainer.frame.height / 2)
        let hitTestView = likeContainer.hitTest(fixYLocation, with: nil)

        if hitTestView is UIImageView {
            UIView.animate(withDuration: 0.3, delay: 0, options: UIView.AnimationOptions.curveEaseInOut, animations: {
                let stackView = self.likeContainer.subviews.first
                stackView?.subviews.forEach({ (image) in
                    image.transform = .identity
                })
                hitTestView?.transform = CGAffineTransform(translationX: 0, y: -50)
            })
        }
    }
    
    
    /// 处理长按结束后的动画
    ///
    /// - Parameter press: UILongPressGestureRecognizer
    fileprivate func handleGestureEnd(press: UILongPressGestureRecognizer) {
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
            let stack = self.likeContainer.subviews.first
            stack?.subviews.forEach({ (image) in
                image.transform = .identity
            })
            self.likeContainer.alpha = 0
            self.likeContainer.transform = self.likeContainer.transform.translatedBy(x: 0, y: 50)
        }) { (_) in
            self.likeContainer.removeFromSuperview()
        }
    }
}

